#!/bin/bash

nfs_host="lanagt"
[ "`hostname`" == "lanagt" ] && nfs_host="lanxpc"
echo "Backup from `hostname` to $nfs_host"

nfs_dir="/pbfd"
nfs_mnt="/backup"
bck_dir="$nfs_mnt/auto_backup"

/sbin/showmount -e "$nfs_host" 1>/dev/null 2>&1
[ $? -ne 0 ] && echo "Backup NFS server not exits, quit!"
/sbin/showmount -e "$nfs_host" 2>/dev/null | grep "$nfs_dir" 1>/dev/null 2>&1
[ $? -ne 0 ] && echo "Backup NFS directory not exits, quit!"

[ ! -d "$nfs_mnt" ] && mkdir -p "$nfs_mnt"
mount "$nfs_host:$nfs_dir" "$nfs_mnt"
[ ! -d "$bck_dir" ] && mkdir -p "$bck_dir"
archive="$bck_dir/`hostname`.tgz"
[ -f "$archive" ] && mv "$archive" "$archive.last"

START=$(date +%s)
tar -cpzf "$archive" \
  --exclude=/dev \
  --exclude=/proc \
  --exclude=/sys \
  --exclude=/tmp \
  --exclude=/run \
  --exclude=/mnt \
  --exclude=/media \
  --exclude=/lost+found \
  --exclude=$nfs_mnt \
  --exclude=/pbfd \
  --exclude=/iscsi \
  --exclude=/var/spool/squid3/cache \
  --exclude=/var/lib/lightdm/.gvfs \
  /
FINISH=$(date +%s)

umount "$nfs_mnt"
echo "total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds"


